package com.example.snackfood.services.impl;

import com.example.snackfood.dao.SnackFoodCartsDao;
import com.example.snackfood.entity.Carts;
import com.example.snackfood.entity.Products;
import com.example.snackfood.services.IsnackFoodCartsService;
import com.example.snackfood.services.IsnackFoodOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: snackFood
 * @description:
 * @author: lydms
 * @create: 2023-11-24 22:56
 **/

@Service
public class SnackFoodCartsService implements IsnackFoodCartsService {
    @Autowired
    private SnackFoodCartsDao snackFoodCartsDao;

    @Override
    public void saveCarts(Carts carts) {
        snackFoodCartsDao.save(carts);
    }

    @Override
    public void deleteCarts(Long id) {
        Query query=new Query(Criteria.where("id").is(id));
        snackFoodCartsDao.delete(query);
    }

    public void updateCarts(Carts carts) {
        Query query=new Query(Criteria.where("id").is(carts.getId()));
        Update update=new Update();

        update.set("user_id",carts.getUser_id());
        update.set("items",carts.getItems());

        snackFoodCartsDao.update(query,update);
    }


    public Carts getCarts(Carts carts) {
        Criteria criteriaid=Criteria.where("id").is(carts.getId());
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaid);
        Query query=new Query(orCriteria);
        return  snackFoodCartsDao.get(query);
    }


}
